package com.neuedu.ec.controller;


import com.neuedu.ec.common.CommonResult;
import com.neuedu.ec.entity.Goods;
import com.neuedu.ec.entity.Orders;
import com.neuedu.ec.service.IOrdersService;
import com.neuedu.ec.vo.OrderQueryVo;
import com.neuedu.ec.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * 订单管理
 * @author 张金山 https://jshand.gitee.io/
 */
@RequestMapping("/auth/order")
@RestController
public class OrdersController {

    @Autowired
    IOrdersService ordersService;

    /**
     * 创建订单
     * @param token 调用wxlogin接口登录成功后服务器返回的令牌
     * @param orderVo
     * @return
     */
    @RequestMapping("/create")
    public CommonResult<Orders> create(@RequestHeader("token") String token ,@RequestBody OrderVo orderVo) throws InvocationTargetException, IllegalAccessException {

        Orders orders = ordersService.create(token,orderVo);
        return CommonResult.success(orders);
    }


    /**
     * 订单支付(支付模拟，仅改变订单状态为已支付)
     * @param token 调用wxlogin接口登录成功后服务器返回的令牌
     * @param orderNumber 订单编号
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @RequestMapping("/pay")
    public CommonResult<Boolean> pay(@RequestHeader("token") String token ,String orderNumber) throws InvocationTargetException, IllegalAccessException {

        Boolean success = ordersService.pay(orderNumber);
        return CommonResult.success(success);
    }


    /**
     * 查询订单列表
     * @param token 用户登录成功获取的token值
     * @param type  1:待付款 2:待发货 其他值(非1、2):全部
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @RequestMapping("/all")
    public CommonResult<Orders> all(@RequestHeader("token") String token ,String type) throws InvocationTargetException, IllegalAccessException {

        List<Orders> list = ordersService.all(token,type);
        return CommonResult.success(new OrderQueryVo(list));
    }


}
